package server;

import SBLib.File;
import SBLib.User;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Listens for replication requests by other RMs
 * 
 * @author BrandonNeal
 */
public class ReplicatorServer extends Thread 
{
    final private ArrayList<File> Files;
    final private ArrayList<User> Users;
    private Replicator replicator;
    
    
    
    public ReplicatorServer(ArrayList<File> files, ArrayList<User> users, Replicator replicator)
    {
        this.Files = files;
        this.Users = users;
        this.replicator = replicator;
        this.start();
        
    }
    @Override
    public void run()
    {
        this.RunReplicatorServer();
    }
    /**
     * - listens for requests. 
     * - When a request occurs, a ReplicationWorker thread is made, which handles the replication.
     */
    private void RunReplicatorServer()
    {
        try 
        {
            ServerSocket listenSocket = new ServerSocket(Server.ReplicatorPort);
            while(true)
            {
                System.out.println("Replication server Listening");
                Socket rmSocket = listenSocket.accept(); //Listen for connections
                System.out.println("Replication Connection Made");
                ReplicatorWorker rWorker = new ReplicatorWorker(rmSocket, this.Files, this.Users);
                
            }
        }
        catch (IOException ex) 
        {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
